package com.bstek.rest.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.controller.RefreshCacheController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.bstek.bdf2.core.business.IUser;

import java.util.List;

@Component
public class LoginIntercepter implements HandlerInterceptor{

	private static final Logger log = LoggerFactory.getLogger(LoginIntercepter.class);

	private List<String> exceptUrls;

	public List<String> getExceptUrls() {
		return exceptUrls;
	}
	public void setExceptUrls(List<String> exceptUrls) {
		this.exceptUrls = exceptUrls;
	}
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
//		System.out.println("I18nUtil............");
//		modelAndView.addObject("I18nUtil", FtlUtil.generateStaticModel(I18nUtil.class.getName()));


	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		String contextPath = request.getContextPath();
		String url =request.getRequestURI();
		if(url.indexOf("login")>0||url.indexOf("Login")>0||url.indexOf("api")>0) {
			//放行登录和退出
			log.info("**********当前url是:{}",url);

			return true;
		}
		IUser user= ContextHolder.getLoginUser();
		if(user!=null){
			return true;
		}else {
			log.info("**********拦截url是:{}",url);
			request.getRequestDispatcher(contextPath+"/login").forward(request,response);
        return false;  
		}
	}

}
